import validator from "validator";

const locale = ['zh-CN']
export default {
  ...validator,
  form:{
    isMobilePhone(rule, value, callback){
      let messgae = rule.message || '请输入正确的手机号'
      if(rule.required || value){
        if(validator.isMobilePhone(value,locale)){
          callback();
        }else{
          callback(new Error(messgae));
        }
      }else{
        callback();
      }

    },
    isEmail(rule, value, callback){
      let messgae = rule.message || '请输入正确的邮箱地址'
      if(rule.required || value) {
        if (validator.isEmail(value)) {
          callback();
        } else {
          callback(new Error(messgae));
        }
      }else{
        callback();
      }
    },
    isStrongPassword(rule, value, callback){
      let messgae = rule.message || '请输入正确的密码（需包含大小写字母和数字,长度8~16位）'
      let options = {
        minLength: 8,
        minLowercase: 1,
        minUppercase: 1,
        minNumbers: 1,
        minSymbols: 0,
      }

      if(rule.required || value) {
        let otherChar = 0
        let char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+'
        for(let i =0; i < value.length; i++){
          if(char.indexOf(value.at(i)) < 0){
            otherChar++
          }
        }
        // 不包含空格
        if(/[ ]/.test(value)){
          callback(new Error(messgae));
        }else
        // 不包含中文
        if(/.*[\u4e00-\u9fa5]+.*$/.test(value)){
          callback(new Error(messgae));
        }else
          // 非法特殊字符
        if(otherChar>0){
          callback(new Error(messgae));
        }else
        // 大小写字母和数字,长度8~16位
        if (validator.isStrongPassword(value,options)) {
          callback();
        } else
        {
          callback(new Error(messgae));
        }
      }else{
        callback();
      }
    }
  }

}
